<?php

class FuneralDirectorForm extends Zend_Form
{
    private $picture_path = "";
	private $can_edit_username;
	private $quick_stats = '';
	private $arranger_tstats = '';
	private $arranger_rstats = '';
	private $branch_tstats = '';
	private $branch_rstats = '';
	private $region_tstats = '';
	private $region_rstats = '';
	private $keystone_tstats = '';
	private $keystone_rstats = '';
    public function __construct($options=array())
    {
        parent::__construct($options);

        // set form name and POST
        $this->setName('user_account');
        $this->setMethod('post');
        $this->setEnctype('multipart/form-data');
		$this->setAction($options['action']);
	
		$this->quick_stats = $options['quick_stats'];	
		if(isset($options['can_edit_username']))
			$this->can_edit_username = true;
		else
			$this->can_edit_username = false;

		$this->arranger_tstats = $options['arranger_timing'];
		$this->arranger_rstats = $options['arranger_revenue'];
		$this->branch_tstats = $options['branch_timing'];
		$this->branch_rstats = $options['branch_revenue'];
		$this->region_tstats = $options['region_timing'];
		$this->region_rstats = $options['region_revenue'];
		$this->keystone_tstats = $options['keystone_timing'];
		$this->keystone_rstats = $options['keystone_revenue'];

		$this->picture_path = $options['picture_path'];
		
        // create basic field decorators
        $basicDecorators = array(
            'ViewHelper',
            array('Errors', array('placement'=>'append')),
            //array(array('labelBr'=>'HtmlTag'), array('tag'=>'br')),
            array('Label', array('placement'=>'prepend')),
            array( array('outerDiv'=>'HtmlTag'), array('tag'=>'div', 'style'=>'margin-top:5px')),
        );

        $picDecorators = array(
            'ViewHelper',
            //array(array('labelBr'=>'HtmlTag'), array('tag'=>'br')),
            array('Label', array('placement'=>'prepend')),
            array( array('outerDiv'=>'HtmlTag'), array('tag'=>'div', 'style'=>'margin-top:5px')),
        );

		$f_license = new Zend_Form_Element_Hidden( 'FDMS_license_number' );
		
		$f_fullname = new Zend_Form_Element_Text( 'full_name', array('size'=>30, 'label'=>'Full Name:', 'required'=>true));
		$f_fullname->addValidator('NotEmpty',true,array('messages'=>array('isEmpty'=>'Required')));
		$f_fullname->setDecorators($basicDecorators);
		
		$f_fname = new Zend_Form_Element_Text( 'fname', array('size'=>30, 'label'=>'First Name:', 'required'=>true));
		$f_fname->addValidator('NotEmpty',true,array('messages'=>array('isEmpty'=>'Required')));
		$f_fname->setDecorators($basicDecorators);
		$f_mname = new Zend_Form_Element_Text( 'mname', array('size'=>30, 'label'=>'Middle Name:','required'=>false));
		$f_mname->setDecorators($basicDecorators);
		$f_lname = new Zend_Form_Element_Text( 'lname', array('size'=>30, 'label'=>'Last Name:','required'=>true));
		$f_lname->addValidator('NotEmpty',true,array('messages'=>array('isEmpty'=>'Required')));
		$f_lname->setDecorators($basicDecorators);
		
		$f_address = new Zend_Form_Element_Text( 'address', array('size'=>48, 'label'=>'Address:','required'=>true));
		$f_address->addValidator('NotEmpty',true,array('messages'=>array('isEmpty'=>'Required')));
		$f_address->setDecorators($basicDecorators);
		$f_city = new Zend_Form_Element_Text( 'city', array('size'=>40, 'label'=>'City:','required'=>true));
		$f_city->addValidator('NotEmpty',true,array('messages'=>array('isEmpty'=>'Required')));
		$f_city->setDecorators($basicDecorators);
		$f_state = new Zend_Form_Element_Text( 'state', array('size'=>2, 'label'=>'State:','required'=>true));
		$f_state->addValidator('NotEmpty',true,array('messages'=>array('isEmpty'=>'Required')));
		$f_state->setDecorators($basicDecorators);
		$f_zip = new Zend_Form_Element_Text( 'zip', array('size'=>10, 'label'=>'Zip:','required'=>true));
		$f_zip->addValidator('NotEmpty',true,array('messages'=>array('isEmpty'=>'Required')));
		$f_zip->setDecorators($basicDecorators);
		
		$f_phone = new Zend_Form_Element_Text( 'phone', array('size'=>21, 'label'=>'Phone:','required'=>true));
		$f_phone->addValidator('NotEmpty',true,array('messages'=>array('isEmpty'=>'Required')));
		$f_phone->setDecorators($basicDecorators);
		$f_email = new Zend_Form_Element_Text( 'email', array('size'=>30, 'label'=>'Email:','required'=>true));
		$f_email->addValidator('NotEmpty',true,array('messages'=>array('isEmpty'=>'Required')));
		$f_email->setDecorators($basicDecorators);
	
		$username = new Zend_Form_Element_Text('username', array('size'=>20, 'label'=>'Username:', 'required'=>true));
		$username->setDecorators($basicDecorators);
		
        $password = new Zend_Form_Element_Text( 'password', array('size'=>20, 'label'=>'Password:', 'required'=>true));
		$password->setDecorators($basicDecorators);

		// add photo upload field
		$f_photo = new Zend_Form_Element_File('photo_image');
        $f_photo->setLabel('Upload a photo:')
			->setRequired(false)
	        ->setDestination(ROOT_DIR.'/upload')
	        ->addValidator('Count', 1)     // ensure only 1 file
	        ->addValidator('Size', false, 502400) // limit to 500K
	        ->addValidator('Extension', false, 'jpg,png,gif'); // only JPEG, PNG, and GIFs
		$f_photo->setDecorators($picDecorators);
				
        // form's submit button
        $submit = new Zend_Form_Element_Submit('submit');
        $submit->setLabel('Submit');

		$this->addElements( array( $f_license ) );
		$this->addElements( array( $f_fullname ) );
		$this->addElements( array( $f_fname, $f_mname, $f_lname ) );
		$this->addElements( array( $f_address, $f_city, $f_state, $f_zip ) );		
		$this->addElements( array( $f_phone, $f_email, $f_photo ) );
	    $this->addElements( array( $username ) );
	    $this->addElements( array( $password ) );
		$this->addElements( array( $submit ) );
    }

    // dirt-simple template engine.
    public function render_with_template( $template_name )
    {
        $template_file_path = APP_DIR."/views/templates/$template_name";
        $template_contents = file_get_contents( $template_file_path );
		
		//TODO - modify this so we can restrict the FD's to editing:
		//  1. name
		//  2. cell phone
		//  3. email
		//  4. picture
		//  5. license number?
		
        // str_replace() keywords, adding errors where needed
        $form_elements = $this->getElements();
		
		$template_contents = str_replace( "{ACTION}", $this->getAction(), $template_contents);
		
		$template_contents = str_replace( "{ENCTYPE}", $this->getEncType(), $template_contents);

        $template_contents = str_replace( "{LICENSE}", $form_elements['FDMS_license_number'], $template_contents );
        $template_contents = str_replace( "{FULLNAME}", $form_elements['full_name'], $template_contents );
        $template_contents = str_replace( "{FNAME}", $form_elements['fname'], $template_contents );
        $template_contents = str_replace( "{MNAME}", $form_elements['mname'], $template_contents );
        $template_contents = str_replace( "{LNAME}", $form_elements['lname'], $template_contents );
		
        //$template_contents = str_replace( "{ADDRESS}", $form_elements['address'], $template_contents );
        //$template_contents = str_replace( "{CITY}", $form_elements['city'], $template_contents );
        //$template_contents = str_replace( "{STATE}", $form_elements['state'], $template_contents );
        //$template_contents = str_replace( "{ZIP}", $form_elements['zip'], $template_contents );		
		
		// should be cell phone...
        $template_contents = str_replace( "{PHONE}", $form_elements['phone'], $template_contents );
        $template_contents = str_replace( "{EMAIL}", $form_elements['email'], $template_contents );		

        $template_contents = str_replace( "{FUNERAL_DIR_IMAGE_PATH}", $this->picture_path, $template_contents );	
        $template_contents = str_replace( "{FUNERAL_DIR_IMG_FILE}", $form_elements['photo_image'], $template_contents );	
		
		if($this->can_edit_username)
		{
	        $template_contents = str_replace( "{USERNAME}", $form_elements['username'], $template_contents );
	        $template_contents = str_replace( "{PASSWORD}", $form_elements['password'], $template_contents );
		}
		else
		{
			$username = "Username: ".$this->username->getValue();
			$password = "Password: ".$this->password->getValue();
	        $template_contents = str_replace( "{USERNAME}", $username, $template_contents );
	        $template_contents = str_replace( "{PASSWORD}", $password, $template_contents );
		}
		
		$template_contents = str_replace( "{ARRANGER_TIMING}", $this->arranger_tstats, $template_contents );
		$template_contents = str_replace( "ARRANGER_REVENUE", $this->arranger_rstats, $template_contents );
		$template_contents = str_replace( "{BRANCH_TIMING}", $this->branch_tstats, $template_contents );
		$template_contents = str_replace( "{BRANCH_REVENUE}", $this->branch_rstats, $template_contents );
		$template_contents = str_replace( "{REGION_TIMING}", $this->region_tstats, $template_contents );
		$template_contents = str_replace( "{REGION_REVENUE}", $this->region_rstats, $template_contents );
		$template_contents = str_replace( "KEYSTONE_TIMING", $this->keystone_tstats, $template_contents );
		$template_contents = str_replace( "{KEYSTONE_REVENUE}", $this->keystone_rstats, $template_contents );
		
		$template_contents = str_replace( "{QUICK_STATS}", $this->quick_stats, $template_contents );
        $template_contents = str_replace( "{SUBMIT_BUTTON}", $form_elements['submit'], $template_contents );

        echo $template_contents;
    }
}

?>
